0. Theory - interview

1. difference between int and big int

![|700](/img/user/Notes/SQL/attachments/Pasted image 20250503140215.png)


2. Difference between char and varchar

The main difference between CHAR and VARCHAR in SQL is how they store and manage string length:

-- CHAR(5)
INSERT INTO test_char VALUES ('Hi');  
-- Stored as: 'Hi   ' (with 3 trailing spaces)

-- VARCHAR(5)
INSERT INTO test_varchar VALUES ('Hi');  
-- Stored as: 'Hi' (no extra spaces)

Use CHAR when values are always the same length (e.g., fixed codes like 'A123'), and VARCHAR when lengths vary (e.g., names, emails).

Yes, CHAR can be slightly faster than VARCHAR โ€” but only in very specific scenarios. Fixed-length makes memory allocation predictable and makes indexing faster


3. what are the types of sql commands

  1. DQL - Data query language Used to retrieve data from databases. (SELECT)
  2. DDL - data definition language Used to create, alter, and delete database objects like tables, indexes, etc. (CREATE, DROP, ALTER, RENAME, TRUNCATE)
  3. DML - data manipulation lang Used to modify the database. (INSERT,UPDATE, DELETE)
  4. DCL - data control language Used to grant & revoke permissions. (GRANT,REVOKE)
  5. TQL - Transaction control language Used to manage transactions. (COMMIT,ROLLBACK, START TRANSACTIONS, SAVEPOINT)

A transaction is a group of SQL operations that are executed as a single unit of work, ensuring data integrity.
Key properties (ACID):

BEGIN TRANSACTION;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT; -- or ROLLBACK;

If an error occurs, you can ROLLBACK to undo all operations.


A foreign key is a column in one table that links to the primary key of another table.
It creates a relationship between two tables.

CREATE TABLE Students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100)
);

CREATE TABLE StudentCourse (
    student_id INT,
    course_id INT,
    FOREIGN KEY (student_id) REFERENCES Students(student_id)
);
Unique key
candidate key

a column or set of columns that can qualify as unique identifier
a table has multiple candidate key but only one is choosen as primary key

alternate key

a candidate key which is not choosen as primary key still unique and can be used


๐Ÿ”น 1. What are constraints in SQL? Name types.

Expected answer:
Constraints are rules applied on columns to enforce data integrity.

Types:


๐Ÿ”น 2. Create a table with all types of constraints.

Question:
Create a students table where:

Answer:

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    marks INT CHECK (marks >= 0 AND marks <= 100),
    email VARCHAR(100) UNIQUE,
    dept VARCHAR(20) DEFAULT 'CSE'
);

๐Ÿ”น 3. What's the difference between UNIQUE and PRIMARY KEY?

Feature PRIMARY KEY UNIQUE
Nulls allowed? โŒ No โœ… Yes (1 or more)
Count per table 1 only Many allowed
Purpose Uniquely identifies rows Enforces unique values

๐Ÿ”น 4. Can a table have multiple primary keys?

Correct answer: No. A table can only have one PRIMARY KEY, but it can consist of multiple columns (called a composite key).

CREATE TABLE attendance (
    student_id INT,
    date DATE,
    PRIMARY KEY (student_id, date)
);

๐Ÿ”น 5. What is the default constraint?

Sets a default value when no value is provided.

CREATE TABLE users (
    id INT PRIMARY KEY,
    status VARCHAR(10) DEFAULT 'active'
);

๐Ÿ”น 6. How to add a constraint after table creation?

-- Add NOT NULL
ALTER TABLE students
ALTER COLUMN name VARCHAR(50) NOT NULL;

-- Add CHECK
ALTER TABLE students
ADD CONSTRAINT chk_marks CHECK (marks >= 0 AND marks <= 100);

๐Ÿ”น 7. What is a foreign key? Give an example.

Links one table's column to another's primary key.

CREATE TABLE departments (
    dept_id INT PRIMARY KEY,
    dept_name VARCHAR(50)
);

CREATE TABLE employees (
    emp_id INT PRIMARY KEY,
    emp_name VARCHAR(50),
    dept_id INT,
    FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);

๐Ÿ”น 8. What happens if you try to insert duplicate in a UNIQUE column?

You get a constraint violation error โ€” SQL Server won't allow it.


๐Ÿ”น 9. Can you disable constraints temporarily?

Yes, in SQL Server:

ALTER TABLE students NOCHECK CONSTRAINT ALL;

But this is not recommended unless you're doing controlled bulk operations.


๐Ÿ”น 10. Whatโ€™s the order of constraint evaluation?

When inserting data, constraints are checked in this rough order:

This helps when debugging constraint errors.


๐Ÿ”ถ 1. Composite Keys vs Surrogate Keys

Question: What are the pros and cons of using composite primary keys over surrogate (auto-increment) keys in large databases?

What they look for:


๐Ÿ”ถ 2. Multi-column Unique Constraint

Question: Create a table where email and phone can be duplicated separately, but not together.

Expected:

CREATE TABLE contacts (
    id INT PRIMARY KEY,
    email VARCHAR(100),
    phone VARCHAR(20),
    CONSTRAINT unique_email_phone UNIQUE (email, phone)
);

๐Ÿ”ถ 3. Complex CHECK Constraints

Question: Allow students to have marks only if their age is at least 18.

Expected:

CHECK (age >= 18 OR marks IS NULL)

They want to see if you can implement business logic using constraints.


๐Ÿ”ถ 4. Self-Referential Foreign Key

Question: Create a table employees where manager_id is a foreign key to employee_id in the same table.

Expected:

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(50),
    manager_id INT,
    FOREIGN KEY (manager_id) REFERENCES employees(employee_id)
);

Follow-up: What happens if a manager is deleted?


๐Ÿ”ถ 5. Cyclic or Cascading Deletes

Question: What happens if two tables have circular foreign keys with ON DELETE CASCADE?

They're checking if you understand referential cycles and danger of accidental mass deletions.


๐Ÿ”ถ 6. Add a constraint that a student's marks must be higher than all others in their dept

This canโ€™t be done with a simple CHECK. You'd need:

This shows you understand limitations of constraints.


๐Ÿ”ถ 7. Whatโ€™s the difference between UNIQUE constraint vs DISTINCT in a SELECT?


๐Ÿ”ถ 8. Why shouldn't we use NULL in a PRIMARY KEY?


๐Ÿ”ถ 9. Enforcing ENUM-like values without ENUM keyword

How would you restrict a gender column to only 'M', 'F', 'O' in SQL Server?

Expected:

CHECK (gender IN ('M', 'F', 'O'))

๐Ÿ”ถ 10. Design Question: What constraints would you apply to a bank_accounts table?

This is open-ended. A good answer would include: